#include "clientthread.h"
#include <QDebug>
#include <QHostAddress>
#include <QApplication>
ClientThread::ClientThread(ushort port,QString ip,QString filePath,QObject *parent) : QThread(parent)
{
    m_ip = ip;
    m_port = port;
    m_filePath = filePath.section("##",0,0);
    m_time = filePath.section("##",1,1);
    m_sendOk = false;
}

void ClientThread::disConnectTcp()
{
    m_tcpSocket->disconnectFromHost();
}

void ClientThread::run()
{
    m_tcpSocket = new QTcpSocket(this);
    connect(m_tcpSocket,SIGNAL(disconnected()),this,SLOT(dealDisconnect()));
    connect(m_tcpSocket,SIGNAL(connected()),this,SLOT(dealConnect()));
    connect(m_tcpSocket,SIGNAL(readyRead()),this,SLOT(dealRead()));
    m_tcpSocket->connectToHost(QHostAddress(m_ip),m_port);

    QFileInfo info(m_filePath);
    m_fileName = info.fileName();
    m_fileSize = info.size();
    QString msg = QString("%1##%2").arg(m_fileName).arg(m_fileSize);
    qDebug()<<"head:"<<msg;
    /*发送文件名和文件大小信息，*/
    m_tcpSocket->write(msg.toUtf8().data(),1024);
    QApplication::processEvents();/*实时发送*/
    /*等待确定再发具体内容 发送文件*/

    m_fileNameTime = m_fileName + "##"+m_time;
    sendFile();

    exec();/*进入事件循环*/
}

void ClientThread::dealDisconnect()
{
//    qDebug()<<"dealDisconnect";
    if(m_sendSize != m_fileSize){
        m_stop = true;/*中途断开,就不发了*/
        emit sigOver(m_fileNameTime,false);
    }else if(!m_sendOk){
        emit sigOver(m_fileNameTime,false);/*接收没成功*/
    }else{
        emit sigOver(m_fileNameTime,true);
    }
}

void ClientThread::dealConnect()
{
    qDebug()<<"dealConnect";

}

void ClientThread::dealRead()
{
//    qDebug()<<"dealRead";
    QTcpSocket* tcp = (QTcpSocket*)sender();
    char buf[1024] = {0};
    qint64 len = tcp->read(buf,1024);
    QString flag = QString("%1").arg(buf);
    qDebug()<<"flag:"<<flag;

    /*判断接收端是否接收成功*/
    if("sucess" == flag){
        m_sendOk = true;
    }else{
        m_sendOk = false;
    }
//    /*好的，收到，通知接收端可以断开了*/
//    len = tcp->write(QString("ok").toUtf8().data(),1024);
//    qDebug()<<"len:"<<len;
}

void ClientThread::sendFile()
{
    m_file.setFileName(m_filePath);
     if(!m_file.open(QIODevice::ReadOnly)){
         return;
     }
    qint64 len=0;
    m_sendSize=0;
    m_stop = false;
    do{
        char buf[4*1024] = {0};
        len = m_file.read(buf,sizeof(buf));
        m_tcpSocket->write(buf,len);
        QApplication::processEvents();/*实时发送*/
        msleep(10);/*发的太快，防止丢包*/
        m_sendSize+=len;
        /*更新进度条*/
        if(len > 0)
            emit sigFileInfo(m_fileNameTime,m_sendSize);
    }while(len>0 && !m_stop);

    /*关闭文件*/
    m_file.close();
}
